package com.example.k8s.hello.service.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.BCrypt;
import com.example.k8s.hello.common.exception.ApiExEnum;
import com.example.k8s.hello.common.bean.Page;
import com.example.k8s.hello.common.utils.StpExtendUtil;
import com.example.k8s.hello.service.sys.service.SysUserRoleService;
import com.example.k8s.hello.service.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import com.example.k8s.hello.common.annotation.SysLog;
import com.example.k8s.hello.common.bean.LoginUser;
import com.example.k8s.hello.bean.sys.entity.SysUserEntity;
import com.example.k8s.hello.bean.sys.form.PasswordForm;
import com.example.k8s.hello.bean.sys.form.SysUserAddForm;
import com.example.k8s.hello.bean.sys.form.SysUserUpdateForm;
import com.example.k8s.hello.bean.sys.query.SysUserQuery;
import com.example.k8s.hello.bean.sys.vo.SysUserVo;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Tag(name = "系统用户")
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController {
	private final SysUserService sysUserService;
	private final SysUserRoleService sysUserRoleService;

	/**
	 * 用户分页
	 */
	@Operation(summary = "用户分页")
	@GetMapping("/list")
	@SaCheckPermission("sys:user:list")
	public Page<SysUserVo> list(@ParameterObject SysUserQuery query, @ParameterObject Page page){
		return sysUserService.queryPage(query, page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@Operation(summary = "获取登录的用户信息")
	@GetMapping("/info")
	public LoginUser info(){
		return StpExtendUtil.getLoginUser();
	}
	
	/**
	 * 修改密码
	 */
	@Operation(summary = "修改密码")
	@SysLog("修改密码")
	@PostMapping("/password")
	public void password(@RequestBody PasswordForm form){
		ApiExEnum.A0010.assertNotEmpty(form.getNewPassword(), "新密码不为能空");

		String password = BCrypt.hashpw(form.getPassword());
		String newPassword = BCrypt.hashpw(form.getNewPassword());

		//更新密码
		boolean flag = sysUserService.updatePassword(StpExtendUtil.getUserId(), password, newPassword);
		ApiExEnum.A0001.assertTrue(flag, "原密码不正确");
	}
	
	/**
	 * 用户详情
	 */
	@Operation(summary = "用户详情")
	@GetMapping("/info/{userId}")
	@SaCheckPermission("sys:user:info")
	public SysUserEntity info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.getById(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return user;
	}
	
	/**
	 * 保存用户
	 */
	@Operation(summary = "保存用户")
	@SysLog("保存用户")
	@PostMapping("/save")
	@SaCheckPermission("sys:user:save")
	public void save(@RequestBody @Validated SysUserAddForm form){
		sysUserService.saveUser(form);
	}
	
	/**
	 * 修改用户
	 */
	@Operation(summary = "修改用户")
	@SysLog("修改用户")
	@PostMapping("/update")
	@SaCheckPermission("sys:user:update")
	public void update(@RequestBody SysUserUpdateForm form){
		sysUserService.update(form);
	}

	/**
	 * 用户批量导入
	 */
	@Operation(summary = "用户批量导入", description = "返回导入成功的数量")
	@PostMapping("/import")
	@SaCheckPermission("sys:user:save")
	public Integer batchImport(@RequestParam("file") MultipartFile file) throws Exception {
		ApiExEnum.A0010.assertTrue(!file.isEmpty(), "上传文件不能为空");

		return sysUserService.batchImport(file.getInputStream());
	}

	/**
	 * 导入模板下载
	 */
	@Operation(summary = "导入模板下载")
	@GetMapping("/template")
	public void templateDownload() throws Exception {
		sysUserService.templateDownload();
	}

	/**
	 * 删除用户
	 */
	@Operation(summary = "删除用户")
	@SysLog("删除用户")
	@PostMapping("/delete")
	@SaCheckPermission("sys:user:delete")
	public void delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			ApiExEnum.A0013.thrown("系统管理员不能删除");
		}
		
		if(ArrayUtils.contains(userIds, StpExtendUtil.getUserId())){
			ApiExEnum.A0013.thrown("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(userIds);
	}
}
